package cn.itsource.user.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.itsource.system.domain.Employee;
import cn.itsource.system.domain.Logininfo;
import cn.itsource.system.mapper.LogininfoMapper;
import cn.itsource.user.domain.User;
import cn.itsource.user.mapper.UserMapper;
import cn.itsource.user.query.UserQuery;
import cn.itsource.user.service.IUserService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;

@Service
// Transactional：用来控制事务的注解；readOnly：只读；propagation：事务传播机制
//  Propagation.SUPPORTS：支持当前事务，如果没有当前事务，就以非事务方法执行
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Transactional
    @Override
    public void add(User user) {
        // hutool生成的盐值和加密
        // RandomUtil.randomString 获得一个随机的字符串（只包含数字和字符） ，32位
        String salt = RandomUtil.randomString(32);
        //  SecureUtil.md5 加密
        String md5Psd = SecureUtil.md5(user.getPassword() + salt);

        user.setSalt(salt);
        user.setPassword(md5Psd);

        // 将员工的信息添加到tb_logininfo表里
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        // 类型 - 0管理员，1用户
        logininfo.setType(1);
        // 由于用户的UserName和logininfo的Username字段不一样，所以不能复制，需要重新添加进去
        logininfo.setUsername(user.getUserName());
        logininfoMapper.add(logininfo);

        // 设置user的LogininfoId为logininfo表里当前对象的id
        user.setLogininfoId(logininfo.getId());

        userMapper.add(user);
    }

    @Transactional
    @Override
    public void update(User user) {
        userMapper.update(user);

        // 将user信息添加进tb_logininfo表里
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setId(user.getLogininfoId());
        logininfo.setType(1);
        logininfo.setUsername(user.getUserName());
        logininfoMapper.update(logininfo);
    }

    @Transactional
    @Override
    public void delete(Long id) {
        User user = userMapper.getOne(id);
        logininfoMapper.delete(user.getLogininfoId());

        userMapper.delete(id);

    }

    @Override
    public User getOne(Long id) {
        return userMapper.getOne(id);
    }

    @Override
    public List<User> getAll() {
        return userMapper.getAll();
    }

    @Transactional
    @Override
    public void patchDelete(Long[] ids) {
        userMapper.patchDelete(ids);
    }

    @Override
    public PageInfo<User> pageQuery(UserQuery query) {
        //原生的写法：1.先查询总页数  2.再根据分页的信息查询当前页的数据
        //PageHelper的写法：开启分页
//                PageHelper.startPage(当前页,每页显示的条数);
        PageHelper.startPage(query.getCurrentPage(), query.getPageSize());
        //分页查询：只要开启了分页，它会帮助我们做什么事情？
        //1.先查询总数量  //2.根据application.yml中驱动名自动实现分页，如果是mysql就用limit
        //3.自动计算limit的第一个值：limit a,b [a = 当前页的起始下标 = (当前页-1)*每页显示的条数]
//        List<Emp> emps = empMapper.pageQuery(empQuery);
        List<User> list = userMapper.pageQuery(query);
        return new PageInfo(list);
    }
}
